package org.dromara.myprogram.utils;

import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dromara.myprogram.domain.MyUnionlotto;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @Description //TODO
 * @Date 2024/8/13 11:52
 * @Author 郭建波
 **/
public class HttpGetJson {

    public static List<MyUnionlotto> getAllUnionlotto(int pageNp, int pageSize) throws IOException {

        String url = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=%s&pageSize=%s&week=&systemType=PC";
        String newUrl = String.format(url, 1, 10000);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建HttpGet实例，指定请求URL
        HttpGet httpGet = new HttpGet(newUrl);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            // 读取响应内容
            String responseBody = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = new JSONObject(responseBody);
            //获取总页数
            String pageNum = jsonObject.get("pageNum").toString();
            System.out.println("总页数" + pageNum);
            String total = jsonObject.get("total").toString();
            System.out.println("总数" + total);
            String pageNo = jsonObject.get("pageNo").toString();
            System.out.println("当前页" + pageNo);
//            String pageSize= jsonObject.get("pageSize").toString();
//            // 处理响应内容...
            String json = jsonObject.get("result").toString();
            Gson gson = new Gson();
            Type userListType = new TypeToken<List<MyUnionlotto>>() {
            }.getType();
            List<MyUnionlotto> myUnionlottos = gson.fromJson(json, userListType);
            return myUnionlottos;


        }
        return null;
    }


}
